package com.koicarp.university.graduate.auth.handle;

import com.alibaba.fastjson.JSONObject;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @auther liutao
 * @Date 2020-08-17 17:48:45
 */
@Component
public class PerAccessDeniedHandler  implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        UniversityResponse response=new UniversityResponse();
        httpServletResponse.setContentType("text/html;charset=utf-8");
        response.code(BaseConstant.NO_PERMISSION_3001);
        response.message("权限不足:  "+e.getMessage());
        //写出流
        PrintWriter out = httpServletResponse.getWriter();
        out.write(JSONObject.toJSONString(response));
        out.flush();
        out.close();
    }
}
